package edu.ccut.saturn.studio.wfg.dialog.informationdialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnInformationNode;

public class SaturnInformationDialog extends TitleAreaDialog {
	
	private Text idText;
	private Text nameText, expressionText, versionText, authorText, descriptionText;
	
	private Label nameLabel, expressionLabel, versionLabel, authorLabel, descriptionLabel;
	
	private SaturnInformationNode saturnInformationNode;

	public SaturnInformationDialog(Shell parentShell, SaturnInformationNode saturnInformationNode) {
		super(parentShell);
		this.saturnInformationNode = saturnInformationNode;
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnInformationNode.getName());
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, false);
		gd_composite.heightHint = 196;
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		final Label idLabel = new Label(composite, SWT.NONE);
		idLabel.setText("ID:");

		idText = new Text(composite, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.verticalIndent = 5;
		idText.setLayoutData(gd_idText);

		nameLabel = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 10;
		nameLabel.setLayoutData(gd_label_1);
		nameLabel.setText(Messages.getString("SaturnInformationDialog.Name"));   //$NON-NLS-1$
		
		nameText = new Text(composite, SWT.BORDER);
		nameText.setEditable(false);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textName.verticalIndent = 10;
		nameText.setLayoutData(gd_textName);
		
		expressionLabel = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 10;
		expressionLabel.setLayoutData(gd_label);
		expressionLabel.setText(Messages.getString("SaturnInformationDialog.Express"));   //$NON-NLS-1$

		expressionText = new Text(composite, SWT.BORDER);
		expressionText.setEditable(false);
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textKey.verticalIndent = 5;
		expressionText.setLayoutData(gd_textKey);
		
		versionLabel = new Label(composite, SWT.NONE);
		final GridData gd_label_2 = new GridData();
		gd_label_2.verticalIndent = 10;
		versionLabel.setLayoutData(gd_label_2);
		versionLabel.setText(Messages.getString("SaturnInformationDialog.Version"));   //$NON-NLS-1$

		versionText = new Text(composite, SWT.BORDER);
		final GridData gd_textVersion = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textVersion.verticalIndent = 5;
		versionText.setLayoutData(gd_textVersion);
		
		authorLabel = new Label(composite, SWT.NONE);
		final GridData gd_label_3 = new GridData();
		gd_label_3.verticalIndent = 10;
		authorLabel.setLayoutData(gd_label_3);
		authorLabel.setText(Messages.getString("SaturnInformationDialog.Author"));   //$NON-NLS-1$

		authorText = new Text(composite, SWT.BORDER);
		final GridData gd_textAuthor = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textAuthor.verticalIndent = 5;
		authorText.setLayoutData(gd_textAuthor);
		
		descriptionLabel = new Label(composite, SWT.NONE);
		final GridData gd_label_4 = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_label_4.verticalIndent = 10;
		descriptionLabel.setLayoutData(gd_label_4);
		descriptionLabel.setText(Messages.getString("SaturnInformationDialog.Decription"));   //$NON-NLS-1$

		descriptionText = new Text(composite, SWT.V_SCROLL | SWT.BORDER);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_textDec.verticalIndent = 5;
		gd_textDec.heightHint = 53;
		descriptionText.setLayoutData(gd_textDec);
		
		idText.setText(saturnInformationNode.getId().trim());
		authorText.setText(saturnInformationNode.getAuthor().trim());
		descriptionText.setText(saturnInformationNode.getDescription().trim());
		expressionText.setText(saturnInformationNode.getKey().trim());
		nameText.setText(saturnInformationNode.getName().trim());
		versionText.setText(saturnInformationNode.getVersion().trim());
		
		return area;
	}
	
	@Override
	protected void buttonPressed(int buttonId) {
		if (saturnInformationNode == null) {
			return;
		}
		if (buttonId == IDialogConstants.OK_ID) {
			// 存到VO里
			saturnInformationNode.setDescription(descriptionText.getText().trim());
			saturnInformationNode.setName(nameText.getText().trim());
			saturnInformationNode.setAuthor(authorText.getText().trim());
			saturnInformationNode.setVersion(versionText.getText().trim());
			saturnInformationNode.setKey(expressionText.getText().trim());
			}
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(500, 350);
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	@Override
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
}
